package com.ruoyi.pfwj.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.pfwj.domain.PfwjFj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.pfwj.domain.Pfwj;
import com.ruoyi.pfwj.service.IPfwjService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 批复文件Controller
 *
 * @author huacai
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/pfwj/pfwj")
public class PfwjController extends BaseController
{
    @Resource
    private IPfwjService pfwjService;

    /**
     * 查询批复文件列表
     */
    @PreAuthorize("@ss.hasPermi('pfwj:pfwj:list')")
    @GetMapping("/list")
    public TableDataInfo list(Pfwj pfwj)
    {
        startPage();
        List<Pfwj> list = pfwjService.selectPfwjList(pfwj);
        return getDataTable(list);
    }

    /**
     * 导出批复文件列表
     */
    @PreAuthorize("@ss.hasPermi('pfwj:pfwj:export')")
    @Log(title = "批复文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Pfwj pfwj)
    {
        List<Pfwj> list = pfwjService.selectPfwjList(pfwj);
        ExcelUtil<Pfwj> util = new ExcelUtil<Pfwj>(Pfwj.class);
        util.exportExcel(response, list, "批复文件数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Pfwj> util = new ExcelUtil<Pfwj>(Pfwj.class);
        util.importTemplateExcel(response, "批复文件数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "批复文件", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('pfwj:pfwj:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Pfwj> util = new ExcelUtil<Pfwj>(Pfwj.class);
        InputStream inputStream = file.getInputStream();
        List<Pfwj> list = util.importExcel(inputStream );
        inputStream.close();
        int count = pfwjService.batchInsertPfwj(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取批复文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('pfwj:pfwj:query')")
    @GetMapping(value = "/{pfwjId}")
    public AjaxResult getInfo(@PathVariable("pfwjId") String pfwjId)
    {
        return success(pfwjService.selectPfwjByPfwjId(pfwjId));
    }

    /**
     * 新增批复文件
     */
    @PreAuthorize("@ss.hasPermi('pfwj:pfwj:add')")
    @Log(title = "批复文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Pfwj pfwj)
    {
        return toAjax(pfwjService.insertPfwj(pfwj));
    }

    /**
     * 修改批复文件
     */
    @PreAuthorize("@ss.hasPermi('pfwj:pfwj:edit')")
    @Log(title = "批复文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Pfwj pfwj)
    {
        return toAjax(pfwjService.updatePfwj(pfwj));
    }

    /**
     * 删除批复文件
     */
    @PreAuthorize("@ss.hasPermi('pfwj:pfwj:remove')")
    @Log(title = "批复文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pfwjIds}")
    public AjaxResult remove(@PathVariable String[] pfwjIds)
    {
        return toAjax(pfwjService.deletePfwjByPfwjIds(pfwjIds));
    }

    /**
     * 获得批复文件主键ID (UUID)
     */
    @GetMapping("/getPfWjId")
    public AjaxResult getPfWjId(){
        String pfwjId = IdUtils.fastSimpleUUID();
        return success(pfwjId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertPfwjFjByPfwjId(@RequestBody PfwjFj pfwjFj){
        return toAjax(pfwjService.insertPfwjFjByPfwjId(pfwjFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectPfwjFjList(String sId){
        PfwjFj pfwjFj = new PfwjFj();
        pfwjFj.setPfwjId(sId);
        return AjaxResult.success(pfwjService.selectPfwjFjList(pfwjFj));
    }

    /**
     * 删除批复文件附件
     */
    @Log(title = "批复文件附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds)
    {
        return toAjax(pfwjService.deletePfwjFjByFjIds(fjIds));
    }

}
